<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Author" content="cjw">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <link type="text/css" rel="stylesheet" href="css/MyCss.css" />
  <link type="text/css" rel="stylesheet" href="css/window_top_style.css" />
  <link type="text/css" rel="stylesheet" href="css/window_main_friends_style.css" />
  <style>
	*{margin: 0; padding: 0;}
	/* S window */
	.window {
		margin:			auto auto; 
		width:			888px; 
		height:			666px; 
		border:			1px solid #ddd; 
		border-radius:	4px; 
		box-shadow:		2px 2px 7px 1px #ccc;
	}
	.window label {
		display:		block;
	}
	/* E window */

	/* S window_main */
	.window > .window_main {
		width:			100%;
		height:			90%;
		overflow:		hidden;
	}
	/* E window_main */

	/* E window_main_chat */
	.window > .window_main > .window_main_chat {
		width:			75%;
		height:			100%;
		float:			left;
		overflow:		hidden;
	}
	
		/* window_main_chat_top */
		.window > .window_main > .window_main_chat > .window_main_chat_top {
			width:		100%;
			height:		5%;
			box-shadow:	0px 0px 10px 1px #ddd;
		}	
		.window_main_chat > .window_main_chat_top > ul {
			height:		100%;
		}
		.window_main_chat > .window_main_chat_top > ul > li {
			float:		left;
			height:		100%;
		}
		.window_main_chat > .window_main_chat_top > ul > li:nth-child(1) {
			width:		96%;

		}
		.window_main_chat > .window_main_chat_top > ul > li:nth-child(2) {
			width:		4%;
		}
		.window_main_chat > .window_main_chat_top > ul > li:nth-child(1) > label{
			font-size:	14px;
			text-align:	center;
			line-height:30px;
			color:		#aba9a9;
		}
		.window_main_chat > .window_main_chat_top > ul > li:nth-child(2) > label{
			float: right;
		}
		.window_main_chat > .window_main_chat_top > ul > li:nth-child(2) > label .chat_close_ico{
			background: url(img/close_orig.png);
			width: 16px;
			height: 16px;
			margin: 7px 6px;
		}
		/* window_main_chat_message */
		.window > .window_main > .window_main_chat > .window_main_chat_message {
			width:		100%;
			height:		75%;
			overflow:	hidden;
			overflow-y: auto;
		}

		.window_main_chat_message::-webkit-scrollbar {width: 4px;border-radius:2px;}
		.window_main_chat_message::-webkit-scrollbar-button {background-color: #fff;}  /* 滑轨两头的监听按钮颜色 */
		.window_main_chat_message::-webkit-scrollbar-track {height:100px;background: #fff;}/*滚动条的滑轨背景颜色 */
		.window_main_chat_message::-webkit-scrollbar-thumb {background: #ccc;border-radius: 5px;}
		
		.window > .window_main > .window_main_chat > .window_main_chat_message > ul > li {
			padding:	2px 20px;
		}

		.window > .window_main > .window_main_chat > .window_main_chat_message > ul > li > .window_main_chat_message_left 
		, .window > .window_main > .window_main_chat > .window_main_chat_message > ul > li > .window_main_chat_message_right{
			margin: 2px 5px;
			width:98%;
		}
		.window_main_chat_message_left > ul, .window_main_chat_message_right > ul {height: 100%;}
		.window_main_chat_message_left > ul > li {
			float: left;
			height:	100%;
		}
		.window_main_chat_message_left > ul > li:nth-child(1) {
			width:	6%;
		}
		.window_main_chat_message_left > ul > li:nth-child(1)
		, .window_main_chat_message_right > ul > li:nth-child(1) > label {
			width: 35px;
			border-radius: 17px;
			overflow: hidden;
			height: 35px;
		}
		.window_main_chat_message_left > ul > li:nth-child(2)
		, .window_main_chat_message_right > ul > li:nth-child(2) {
			width:	2%;
			height:	30px;
		}
		.window_main_chat_message_left > ul > li:nth-child(3)
		, .window_main_chat_message_right > ul > li:nth-child(3) {
			width:	80%;
		}
		.window_main_chat_message_left > ul > li:nth-child(3) > label, .window_main_chat_message_right > ul > li:nth-child(3) > label {
			border-radius: 5px;
			background: #e67029;
			margin-top: 4px;
			color:		#ddd;
			padding:	10px;		
			text-align: center;
		}
		.window_main_chat_message_right > ul > li {
			float:		right;
		}
		.window_main_chat_message_right > ul > li:nth-child(3) > label {
			float:		right;
		}
		/* S window_main_chat_input */
		.window > .window_main > .window_main_chat > .window_main_chat_input {
			width:		100%;
			height:		20%;
			box-shadow:	0px 0px 10px 1px #ccc;
		}
		/* window_main_chat_input_control */
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_control {
			width:		100%;
			height:		20%;
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_control  > ul {
			height:		100%;
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_control  > ul > li {
			float:		left;
			width:		25px;
			height:		100%;
		}
		.window_main_chat > .window_main_chat_input i {
			width:		20px;
			height:		20px;
			cursor:		pointer;
			margin:		2px 10px;
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_control  > ul > li .face_control_ico {
			background:	url(img/face_orig.png);
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_control  > ul > li .face_control_ico:hover	{
			background:	url(img/face_hover.png);
		}

		.phon_control_ico {
			background:	url(img/phon_orig.png);
			width:		20px;
			height:		20px;
		}
		.phon_control_ico:hover	{
			background:	url(img/phon_hover.png);
		}
		/* window_main_chat_input_infor */
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_infor	{
			width:		100%;
			height:		60%;
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_infor > label {
			height:		88%;
			margin:		0px 8px;
			outline:	none;
			border:		1px solid #f1eeee;
			border-radius: 5px;
			padding:	4px;
			color:		#aba9a9;
			font-size:	14px;
			overflow:	hidden;
			overflow:	auto;	
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_infor > label::-webkit-scrollbar {width: 4px;border-radius:2px;}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_infor > label::-webkit-scrollbar-button {background-color: #fff;}  /* 滑轨两头的监听按钮颜色 */
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_infor > label::-webkit-scrollbar-track {height:100px;background: #fff;}/*滚动条的滑轨背景颜色 */
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_infor > label::-webkit-scrollbar-thumb {background: #ccc;border-radius: 5px;}
		
		/* window_main_chat_input_send */
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_send {
			width:		100%;
			height:		20%;
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_send > label {
			width:		30px;
			float:		right;
			margin-right: 11px;
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_send > label > .chat_send_ico {
			background:	url(img/send_orig.png);
		}
		.window_main_chat > .window_main_chat_input > .window_main_chat_input_send > label > .chat_send_ico:hover {
			background:	url(img/send_hover.png);
		}

		.face_cnt{position:absolute; width:366px; height:53px; margin:26px 10px; box-shadow:0px 0px 4px #ddd; background:#fff; display:none; cursor:pointer; overflow:hidden; overflow-y:auto;}
		.face_cnt ul{list-style-type:none;}
		.face_cnt ul li {float:left; margin:2px 5px; }
		.face_cnt ul li img{border:1px solid #fff; border-radius:2px; padding:2px; border-radius:4px;}
		.face_cnt ul li img:hover{border:1px solid #ddd;}
		.face_cnt::-webkit-scrollbar {width: 4px;border-radius:2px;}
		.face_cnt::-webkit-scrollbar-button {background-color: #fff;}  /* 滑轨两头的监听按钮颜色 */
		.face_cnt::-webkit-scrollbar-track {height:100px;background: #fff;}/*滚动条的滑轨背景颜色 */
		.face_cnt::-webkit-scrollbar-thumb {background: #ccc;border-radius: 5px;}
		/* E window_main_chat_input */

	/* E window_main_chat */
  </style>
 </head>
 <body>
	<!-- S window -->
	<div class="window">
		<!-- S window_top -->
		<div class="window_top">
			<ul class="nolist">
				<li>
					<label class="head_portrait">
						<img alt="" src="" width="60px" height="60px"/>
					<label>
				</li>
				<li>
					<label class="serch">
						<i class="serch_ico"></i>
					</label>
				</li>
				<li></li>
				<li>
					<label class="right_control">
						<ul class="nolist">
							<li></li>
							<li></li>
							<li></li>
							<li>
								<label>
									<i class="close_ico"></i>
								</label>
							</li>
						</ul>
					</label>
				</li>
			</ul>
		</div>
		<!-- E window_top -->

		<!-- S window_main -->
		<div class="window_main">
			<!-- S window_main_friends -->
			<div class="window_main_friends">
				<ul class="nolist">
					<!--
					<li>
						<label>
							<label>
								<label class="friend_pho">
									<img class="friend_pho_img" alt="" src="img/friend_cjw.png" width="45px" height="45px"/>
								</label>
							</label>
							<label>
								<label >
									<label class="friend_username">哥</label>
									<label class="friend_now_msg">好的</label>
								</label>
							</label>
							<label>
								<label class="friend_msg_num">
									99
								</label>
								<label class="friend_close">
									<i class="friend_close_ico"></i>
								</label>
							</label>
						</label>
					</li>
					-->
				</ul>
			</div>
			<!-- E window_main_friends -->

			<!-- S window_main_chat -->
			<div class="window_main_chat">
				<!-- window_main_chat_top -->
				<div class="window_main_chat_top">
					<ul class="nolist">
						<li>
							<label class="username">哥</label>
						</li>
						<li>
							<label class="chat_close">
								<i class="chat_close_ico"></i>
							</label>
						</li>
					</ul>
				</div>
				<!-- window_main_chat_message -->
				<div class="window_main_chat_message">
					<!--<ul class="nolist">
						<li>
							<label class="window_main_chat_message_left clearFloat"> 
								<ul class="nolist">
									<li>
										<label>
											<img alt="" src="img/friend_cjw.png" width="35px" height="35px"></img>
										</label>
									</li>
									<li></li>
									<li>
										<label>
											好的#好的
										</label>
									</li>
								</ul>
							</label>
						</li>
					</ul>-->
				</div>
				<!-- S window_main_chat_input -->
				<div class="window_main_chat_input">
					<!-- window_main_chat_input_control -->
					<div class="window_main_chat_input_control">
						<div class="face_cnt"></div>
						<ul class="nolist">
							<li>
								<i class="face_control_ico"></i>
							</li>
							<li>
								<i class="phon_control_ico"></i>
							</li>
							<li></li>
						</ul>
					</div>
					<!-- window_main_chat_input_infor -->
					<div class="window_main_chat_input_infor">
						<label class="input_some" contenteditable="true">
						</label>
					</div>
					<!-- window_main_chat_input_send -->
					<div class="window_main_chat_input_send">
						<label class="chat_send">
							<i class="chat_send_ico"></i>
						</label>
					</div>
				</div>
				<!-- E window_main_chat_input -->
			</div>
			<!-- E window_main_chat -->
		</div>
		<!-- E window_main -->
	</div>
	<!-- E window -->
	<script src="js/MudelData.js"></script>
	<script src="js/MyjsV0-8.js"></script>
	<script src="js/Data.js"></script>
	<script>
	console.log("cjw");
	/* S window_main_chat */
	var flag3 = true; //表情按钮逻辑
	var faceAni;
	//点击出现表情框
	/*
	聊天界面 消息框
	dealMessage.deal(json);
	*/
	var dealMessage = {
		//入口 0(type)-接收; 1(type)-发送
		deal:function (obj){
			if(obj.getRe("msg_type") == 0)
				dealMessage.insert(Mudel.leftMessageMudel(dealMessage.dealMsg(obj.getRe("msg")), obj.getRe("h_pho"), obj.getRe('msg')));
			else 
				dealMessage.insert(Mudel.rightMessageMudel(dealMessage.dealMsg(obj.getRe("msg")), obj.getRe("h_pho"), obj.getRe('msg')));
		},
		//插入
		insert:function(html){
			$(".window_main_chat_message")[0].$("ul")[0].insert(html);
		},
		//判断消息框大小
		dealMsg:function(message){
			var ch = '';
			var width = 0;
			for(var i = 0; i < message.length; i++){
				if(width >= 456) return 456;
				ch = message.charAt(i);
				//判断表情
				if(ch == '['){
					temp = i;
					i = message.indexOf("]", i);
					var keyOfFace = message.substring(temp, i + 1);
					var valueOfFace = faceMapPath.get(keyOfFace);
					if(valueOfFace != undefined){
						width += 20;
						continue;
					}
					i = temp;
				}
				else if(ch == ' ' || $$.util.isEnglish(ch) || 
					$$.util.isHalfSymbol(ch) || $$.util.isNumber(ch))
					width += 7;
				else if($$.util.isChinese(ch) || $$.util.isAllSymbol(ch))
					width += 12;
				else
					new Exception("dealMessage", "dealMsg", "ch = " + ch + "意外字符！");
			}
			return width < 12 ? 12 : width;
		},
		//解析输入框中的表情
		dealFace:function(msg){
			if(msg.indexOf("[") != -1){
				var con = ""; 
				var start = 0;
				var selectIndex = 0;
				var temp;
				while((start = msg.indexOf("[", start)) != -1){
					con += msg.substring(selectIndex, start);
					temp = msg.indexOf("]", start) + 1;
					var keyOfFace = msg.substring(start, temp);
					var valueOfFace = faceMapPath.get(keyOfFace);
					con += valueOfFace != undefined 
						? Mudel.faceMudel(valueOfFace) 
						: keyOfFace;
					selectIndex = start = temp;
				}
				con += msg.substring(selectIndex, msg.length);
				return con;
			}
			return msg;
		},
	};
	
	
	/* E window_main_chat */

	/* S window_main_friends */
	/*

	好友信息
	{user:"1281384046", d_name:"哥" ,u_name:"IA", h_pho:"img/friend_cjw.png", historyMsg:[],}
	{user:"0000000000", d_name:, u_name:"图灵AI", h_pho:"img/frend_ai.png", historyMsg:[],}
	
	currentSession	当前会话			存在
	hSessionList	打开不是当前会话集	存在
	nSessionList	未打开会话集		不存在
	compSessionMap	组件对session表
	user_sessionMap 用户对session表

	void createSession(JSON);//接收JSONObject JSONArray
	void setCurrentSession(Session);//
	Session getCurrentSession();
	void putCompSession(Comp, Sesion)
	Session getSessionByComp(Comp)
	void putUserSessionMap(String, Session)
	Session getSessionByUser(User)

	session 
		user		用户名
		name		昵称
		compOfBt	按钮
		compOfChat	聊天界面
		start 创建窗口，设置为当前会话
		stop 暂停会话，隐藏当前会话框
		continue	恢复会话，
		close 关闭会话，删除相关的界面、
		reci		接收来自服务器的信息并显示--》处理
		send		在界面显示--》发送服务器 针对currentSession对象
		historyCount 
		JSONArray historyMsg 初始化时使用

	初始化
	实例化所有会话--》装进nSessionList集中

	操作
		点击按钮
			涉及对象：currentSession、hSessionList、nSessionList
			currentSession：无作用
			hSessionList：
				1、存在currentSession，将currentSession暂停
				2、取出reSession，赋值currentSession
				3、是否存在历史消息，清除提示清空历史消息数量
			nSessionList：
				1、存在currentSession，将currentSession暂停
				2、取出openSession,赋值currentSession 
				3、是否存在历史消息，显示存在的消息，清除提示清空历史消息数量
		收
			涉及对象：currentSession、hSessionList、nSessionList
			currentSession：
			hSessionList：
				1、计算信息的数量-进行提醒
				2、插入界面
			nSessionList：
				1、计算信息的数量-进行提醒
				2、将信息进行缓存
		发
			涉及对象：currentSession
			currentSession：
				1、
	*/
	$(".window_main_chat")[0].style.display = "none";
	var manage = new ManageSession();
	var msgListener = new MsgListener()
	function ManageSession(){
		var currentSession;	//当前会话			存在
		var hSessionList = new ArrayList();	//打开不是当前会话集	存在
		var nSessionList = new ArrayList();	//未打开会话集		不存在
		var userSessionMap = new Map(); //用户对session表
		this.createSession = function(jsonObject){
			creSession(jsonObject);
		}
		//数据类型：{infor:{user, d_name, h_pho}, historyMsg:[], allMessage:[]}
		function creSession(jsonObject){
			var session = new Session();
			var infor = jsonObject.get("infor");
			session.setInfor(infor);
			session.setCompOfBt(creFriendsComp(infor.get("user"), 
				infor.get("d_name"), infor.get("h_pho")));
			session.setHistoryMsg(jsonObject.get("historyMsg"));
			session.setAllHistoryMsg(jsonObject.get("allMessage"));
			session.addListener(msgListener);
			nSessionList.add(session);
			console.log(infor.get("user"));
			userSessionMap.put(infor.get("user"), session);
			session.wait();
		}
		function creFriendsComp(user, name, h_pho) {
			return $(".window_main_friends")[0].$("ul")[0].insert(Mudel.friendsItemMudel(user, name, h_pho));
		}
		this.isCurrentSession = function(session){
			if(currentSession == null) return false;
			return currentSession.getInfor().get("user") == session.getInfor().get("user");
		}
		this.setCurrentSession = function(session){
			if(currentSession != undefined)
				if(!hSessionList.serch(currentSession)) hSessionList.add(currentSession);
			currentSession = session;
		}
		this.getCurrentSession = function(){
			return currentSession;
		}
		this.putCompSession = function(comp, sesion){
			compSessionMap.put(comp, sesion);
		}
		this.putUserSessionMap = function(user, session){
			userSessionMap.put(user, session);
		}
		this.getSessionByComp = function(comp){
			return compSessionMap.get(comp);
		}
		this.getSessionByUser = function(user){
			return userSessionMap.get(user);
		}
		this.removeSession = function(session){
			if(hSessionList.remove(session) != true)
				if(nSessionList.remove(session) != true)
					new Exception("ManageSession", "removeSession", "会话不能正常删除session=" + session);
		}
		this.clickStartChat = function(user){
			var session = this.getSessionByUser(user);
			if(currentSession != session){
				if(hSessionList.serch(session)){
					session.stop();
					session.wait();
					this.setCurrentSession(session);
					this.getCurrentSession().continue();
				}else if(nSessionList.serch(session)){
					session.stop();
					session.wait();
					this.setCurrentSession(session);
					this.getCurrentSession().start();
				}else 
					new Exception("ManageSession", "click", "集合中不存在会话:user=" + session.getInfor().get("user"));
				
			}
		}
		this.clickHideChat = function(){
			hSessionList.add(this.getCurrentSession());
			this.setCurrentSession(null);
		}
		this.clickCloseChat = function(comp){
			var session = this.getSessionByComp(comp);
			if(currentSession == session){
				session.close();
				session.wait();
				nSessionList.add(session);
				this.getCurrentSession() = null;
			}else if(hSessionList.serch(session)){
				session.close();
				session.wait();
				nSessionList.add(session);
			}else {
				session.close();
				session.wait();
			}
		}
	}
	function Session(){
		var infor;					//当前session用于信息
		var compOfBt;				//按钮
		var compOfChat;				//聊天界面
		var historyCount = 0;		//历史消息数量
		var historyMsg;				//历史消息 初始化时使用 
		var msgListener;			//监听 监听信息数量、监听接收消息、监听发送消息
		var inputValue;				//当前session所占用的输入框的值
		var allHistoryMsg;			//所有消息

		this.getCompOfBt = function(){
			return compOfBt;
		}
		this.getCompOfChat = function(){
			return compOfChat;	
		}
		this.getHistoryMsg = function(){
			return historyMsg;
		}
		this.getInputValue = function(){
			return inputValue;
		}
		this.getAllHistoryMsg = function(){
			return allHistoryMsg;
		}
		this.reHistoryCount = function(){
			historyCount = 0;
		}

		this.getInfor = function(){
			return infor;
		}
		this.setInfor = function(_infor){
			infor = _infor;
		}
		this.setCompOfBt = function(compOfBt_){
			compOfBt = compOfBt_;
		}
		this.setHistoryMsg = function(historyMsg_){
			historyMsg = historyMsg_;
		}
		this.setInputValue = function(_inputValue){
			inputValue = _inputValue
		}
		this.setAllHistoryMsg = function(_allHistoryMsg){
			allHistoryMsg = _allHistoryMsg
		}
		
		//待激活
		this.wait = function(){
			if(historyMsg != null && historyMsg.length() > 0 && msgListener != null){
				compOfBt.$(".friend_msg_num")[0].style.display = "block";
				inputValue = $(".input_some")[0].text();
				$(".input_some")[0].text("");
				msgListener.countObserve(this, historyMsg.length());
				historyMsg.forEach(function(item){
					allHistoryMsg.add(parseMsgObjectOfSave(item.get("msg"), item.get("msg_type")));
				});
				historyMsg.removeAll();
			}
			if(allHistoryMsg.length() > 0){
				var message = allHistoryMsg.getJSONObject(allHistoryMsg.length()-1).get("msg");
				msgListener.nowMsgObserve(this, message);
			}
		}

		//激活会话//创建窗口，设置为当前会话
		this.start = function(){
			$(".window_main_chat")[0].style.display = "block";
			compOfBt.$(".friend_msg_num")[0].style.display = "none";
			$(".input_some")[0].text(inputValue == undefined ? "" : inputValue);
			reFrame(infor.get("d_name"));
			this.reHistoryCount();
			if(allHistoryMsg != null){
				allHistoryMsg.forEach(function(item){
					dealMessage.deal(parseMsgObject(item.get("msg"), item.get("msg_type")));
				});
			}
			scroll();
		}	

		//暂停会话，隐藏当前会话框
		this.stop = function(){
			$(".window_main_chat")[0].style.display = "none";
			inputValue = $(".input_some")[0].text();
			$(".input_some")[0].text("");
			if(compOfChat != null)
				compOfChat = compOfChat.remove();
		}
		
		//恢复会话，
		this.continue = function(){
			$(".window_main_chat")[0].style.display = "block";
			compOfBt.$(".friend_msg_num")[0].style.display = "none";
			$(".input_some")[0].text(inputValue == undefined ? "" : inputValue);
			reFrame(infor.get("d_name"));
			if(allHistoryMsg != null){
				allHistoryMsg.forEach(function(item){
					dealMessage.deal(parseMsgObject(item.get("msg"), item.get("msg_type")));
				});
			}
			this.reHistoryCount();
			scroll();
		}		

		//关闭会话，删除相关的界面、
		this.close = function(){
			if(compOfChat) compOfChat.remove();
			compOfBt.remove();
			$(".window_main_chat")[0].style.display = "none";
			manage.removeSession(this);
		}		

		//接收来自服务器的信息并显示--》处理
		this.reci = function(jsonObject){
			/*
			1、将接收到的信息存档
			2、msgListener不为空时进行消息提示并进行消息接收提示--不是当前会话时进行提醒
			3、判断是否为当前会话 是创建信息框 不是保存进历史
			4、滚动到最底部
			*/
			//1
			if(allHistoryMsg != null) allHistoryMsg.add(parseMsgObjectOfSave(jsonObject.get("msg"), jsonObject.get("msg_type")));
			//2
			if(msgListener != null) {
				msgListener.nowMsgObserve(this, jsonObject.get("msg"));
				//3
				if(!manage.isCurrentSession(this)){
					historyCount++;
					compOfBt.$(".friend_msg_num")[0].style.display = "block";
					msgListener.countObserve(this, historyCount);
					historyMsg.add(parseMsgObject(jsonObject.get("msg"), jsonObject.get("msg_type")));
				}else 
					dealMessage.deal(parseMsgObject(jsonObject.get("msg"), jsonObject.get("msg_type")));
			}
			
			//4
			scroll();
		}		

		//在界面显示--》发送服务器 针对currentSession对象
		this.send = function(json){
			dealMessage.deal(json);
			allHistoryMsg.add(parseMsgObjectOfSave(json.get("msg"), json.get("msg_type")));
			
			if(msgListener != null) 
				msgListener.nowMsgObserve(this, json.getRe("msg"));
			scroll();
		}		

		this.addListener = function(l){
			msgListener = l;
		}

		//continue/start
		function reFrame(name){
			compOfChat = $(".window_main_chat_message")[0].insert(Mudel.chatFrameMudel(name));
			$(".window_main_chat")[0].$(".username")[0].text(name);
		}
		//消息框创建 -- 数据对象
		function parseMsgObject(msg, type){
			var obj = new JSONObject();
			obj.put("msg_type", type);
			obj.put("msg", msg);
			obj.put("h_pho", DataBase.get("imgs").get(infor.get("user")));
			return obj;
		}
		//消息保存 -- 数据对象
		function parseMsgObjectOfSave(msg, type){
			var obj = new JSONObject();
			obj.put("msg_type", type);
			obj.put("msg", msg);
			return obj;
		}

		this.toString = function(){
			console.log(infor.get("d_name") + " " + infor.get("user") + " " + compOfBt + " " + compOfChat);
		}
	}

	function MsgListener(){
		this.countObserve = function(session, count){
			session.getCompOfBt().$(".friend_msg_num")[0].text(count);
		}
		this.nowMsgObserve = function(session, msg){
			session.getCompOfBt().$(".friend_now_msg")[0].text(msg);
		}

	}

	/* E window_main_friends */
	

	//设置有滚动的窗口滚动到最底部
	function scroll(){
		$(".window_main_chat_message")[0].scrollTop = $(".window_main_chat_message")[0].scrollHeight;
	}

	/*
	服务器和前台交接处 -- 管理交互
	服务器--》前台
	负责调用manage去获取session session.reci方法
	前台--》服务器
	session.send 通过ManageAjax发送到服务器
	*/
	//界面层
	FrameManageAjax = (function() {
		function FrameManageAjax(){
		}
		FrameManageAjax.prototype.init = function(jsonOjbect) { //初始化界面
			//初始化个人信息
			$(".head_portrait")[0].$("img")[0].src = DataBase.get("imgs").get(DataBase.get("infor").get("user"));
			//初始化好友栏
			var friends = DataBase.get("friends");
			friends.forEach(function(item){
				console.log("item-"+item.toString());
				//将数据转换成manage.createSession参数对应的数据{infor:{user, d_name, h_pho}, historyMsg:{msg:[]}, allMessage:{msg:[]}}
				var object = new JSONObject();
				var infor  = new JSONObject();
				var item_ = item;
				item_.put("h_pho", DataBase.get("imgs").get(item.get("user")));
				object.put("infor", item_);
				
				var historyMsgObject;
				if(DataBase.get("historyMsg").get(item.get("user")) != undefined){
					historyMsgObject = DataBase.get("historyMsg").get(item.get("user"))
				} else {
					historyMsgObject = new JSONArray();
					DataBase.get("historyMsg").put(item.get("user"), historyMsgObject);
				}
				object.put("historyMsg", historyMsgObject);

				var allMessageObject;
				if(DataBase.get("allMessage").get(item.get("user")) != undefined){
					allMessageObject = DataBase.get("allMessage").get(item.get("user"));
				} else {
					allMessageObject = new JSONArray();
					DataBase.get("allMessage").put(item.get("user"), allMessageObject);
				}
				object.put("allMessage", allMessageObject);

				console.log("object-"+object.toString());
				manage.createSession(object);
			});
			//初始化表情栏
			var values = faceMapPath.getValues();
			var faceMudle = '';
			for(var i = 0; i < values.length; i ++)
				faceMudle += Mudel.origFaceMudel(values[i]);
			$(".face_cnt")[0].insert("<ul>"+faceMudle+"</ul>");
			//表情动画
			faceAni = new AnimatedFun({
				aniEle:$(".face_cnt")[0],
				aniMin:0,
				aniMax:53,
			});
		};
		FrameManageAjax.prototype.reci = function(jsonObject) {

		};
		FrameManageAjax.prototype.send = function(jsonObject) {
		};
		FrameManageAjax.prototype.save = function(jsonObject) {
		};
		return FrameManageAjax;
	})();
	//服务层
	ServerManageAjax = (function(_super) {
		__extends(ServerManageAjax, _super);
		function ServerManageAjax() {
			return ServerManageAjax._super.constructor.apply(this, arguments);
		}
		ServerManageAjax.prototype.init = function(jsonOjbect){
			ServerManageAjax._super.init.call(this, jsonOjbect);
		};
		ServerManageAjax.prototype.reci = function(jsonOjbect) {
			switch(jsonOjbect.get("type")){
				case 0 : 
					console.log("系统消息");
					break;		//系统
				case 1 : 
					var session = manage.getSessionByUser(jsonOjbect.get("from"));
					session.reci(jsonOjbect);
					break;		//聊天
				case 2 : 
					console.log("请求好友、同意");
					break;		//请求好友、同意
				case 3 : 
					console.log("查找、结果");
					break;		//查找、结果
				default : new Exception("ServerManageAjax", "reci", "消息类型非法");
			}
			
			ServerManageAjax._super.rece.call(this, jsonOjbect);
		};
		ServerManageAjax.prototype.send = function(jsonObject) {
			ServerManageAjax._super.send.call(this, jsonObject);	
		};
		ServerManageAjax.prototype.save = function(jsonObject) {
			ServerManageAjax._super.save.call(this, jsonObject);
		};
		return ServerManageAjax;
	})(FrameManageAjax);
	var manageAjax = new ServerManageAjax(); 
	manageAjax.init();

	//robotChat("hello!");
	function robotChat(send){
		
	}
	//模拟IA发送自动发送信息
	function autoIA(){
		var jsonObject = new JSONObject();
		jsonObject.put("from", "1281384046");
		jsonObject.put("to", "928172252");
		jsonObject.put("type", 1);
		jsonObject.put("msg_type", 0);
		jsonObject.put("msg", "你好！");
		manageAjax.reci(jsonObject);
	}

	$(".face_control_ico")[0].onclick = function(e){
		if(flag3){
			flag3 = false;
			faceAni.run();
		}
		else{
			flag3 = true;
			faceAni.next();
		}
	}
	//点击每个表情返回名字 使用事件委托和冒泡原理  利于优化性能
	$(".face_cnt")[0].addEvent("click", function(e){
		if(e.target.nodeName.toLowerCase() == "img"){
			title = e.target.getAttribute("title");
			$(".input_some")[0].text($(".input_some")[0].text() + "[" + title + "]");
			flag3 = true;
			faceAni.next();
		}
	});
	//好友列表点击
	$(".window_main_friends")[0].onclick = function(e){
		if(e.target.tName() == "label"){
			var ndoe = e.target.parent("li");
			manage.clickStartChat(ndoe.attr("data-id"));
			
		}
	}
	//发送按钮
	$(".chat_send_ico")[0].onclick = function(e){
		if($(".input_some")[0].text().length > 0){
			ajaxC({
				url:"sendAction",
				data:{msg:$(".input_some")[0].text()}, 
				success:function(msg){
					manage.getCurrentSession().send(msg);
					$(".input_some")[0].text("");
				},
				error:function(msg){
					
				},
			});
		}
	}
	//关闭按键点击
	$(".window_top")[0].$(".close_ico")[0].onclick = function(){
		alert(DataBase.toString());
	}
	new RefreshMsg();
	//开启10毫秒接收一次消息
	function RefreshMsg(){
		setInterval(function(){
			ajaxC({
				url:"reciAction",
				success:function(msg){
					if(msg != undefined && msg.size() > 0){
						msg.forEach(function (item){
							var session = manage.getSessionByUser(item.get("from"));
							session.reci(item);
						});
					}
				},
				error:function(msg){
					console.log(msg);
				},
			});
		}, 10);
	}

	var list = new ArrayList();//装当前接收到的信息
	actionConfJson = [{action:"SendAction"}, {action:"ReciAction"}];
	function SendAction(){
		this.execute = function (data){
			var object = parseData.parseSendObject(DataBase.get("infor").get("user"), data.msg);
			print(object);

			//机器人
			if(manage.getCurrentSession().getInfor().get("user") == "000000"){
				ajax({
					type:"post",
					url:'http://www.tuling123.com/openapi/api',
					data:{key:"ddb5a8141f87480ebf3d08a1c8644c1b", info:data.msg},
					success:function(msg){
						//var data1_reci = '{name:"哥", type:0, msg:"好的呢", h_pho:"img/friend_cjw.png"}';
						var json = $$.json.toObject(msg.toString());
						var object = parseData.parseReciObject("000000", json.getRe("text"));
						list.add(object);
					},
					error:function(){
						alert("初始化异常") ;
					},
				});
			}
			//其他好友
			else{
				setInterval(function(){
					var object = parseData.parseReciObject(1281384046, "你好，这是自动发送测试消息！");
					list.add(object);
				}, Num_ran(15000,100000));
			}
			return SUCCESS;
		}
	}
	function ReciAction(){
		this.execute = function(){
			if(list.size() > 0){
				var temp = list;
				print(temp);
				list = new ArrayList();
			} else {
				print("当前没有消息！");
				return ERROR;
			}
			return SUCCESS;
		}
	}

	//获取标准的某一特定情况下的数据格式
	var parseData = {
		parseReciObject:function(fromUser, msg){
			var object = new JSONObject();
			object.put("from", fromUser);
			object.put("type", 1);
			object.put("msg_type", 0);
			object.put("msg", msg);
			return object;
		},
		parseSendObject:function(toUser, msg){
			var object = new JSONObject();
			//object.put("name","self");
			object.put("type",1);
			object.put("msg_type", 1);
			object.put("h_pho", DataBase.get("imgs").get(toUser));
			object.put("msg", msg);
			return object;
		},
	}
	</script>
 </body>
</html>